#!/bin/bash

WRITEQUOTA="/${QUOTA_TEST}/writeQuotaDir"
WRITEDIR="${FS_MOUNTPOINT}${WRITEQUOTA}"
LOGFILE="/tmp/quotalog"
global_used=0

msg0="测试所有用例 (默认会测试对给非空目录创建配额时获取目录大小)";
msg1="测试在<非空配额目录>对已有文件追加写且不超出配额";
msg2="测试在<非空配额目录>对已有文件追加写且超出配额";
msg3="测试在<非空配额目录>对已有文件覆盖写且不超出配额";
msg4="测试在<非空配额目录>对已有文件覆盖写且超出配额";
maxmsg=4

function print_help() {
    name="$0"
    echo "Usage:"
    for (( num=0; num<=${maxmsg}; num++ ));do
        eval echo "'    'sh ${name} ${num} '  ' \${msg${num}}"
    done
    exit
}

function init_all() {
    hard_limit=$(($1 * 1024 * 1024)) # 10 MB
    bs_size=$((2 * 1024 * 1024))   # 2 MB

    clean_quota; #sleep ${WAIT_CLIENT_SYNC};
    mkdir -p ${WRITEDIR}; rm -f ${WRITEDIR}/*; echo -e "=====清空目录===== pass"
    
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size} count=1";echo -e "exec cmd:\n\t${cmd}"; eval ${cmd} &> ${LOGFILE}
    if (($? != 0));then cat ${LOGFILE}; exit 1; fi

    cmd="orcafs-ctl --setquota --dir=${WRITEDIR} --hardlimit=${hard_limit}";echo -n -e "exec cmd:\n\t${cmd}\n\t"; eval ${cmd}
    func_get_quota_used; new_used=${global_used}
    sleep ${WAIT_CLIENT_SYNC};

    echo -n "=====新创建文件然后重新创建配额===== "
    if (( ${new_used} == ${bs_size})); then
        echo -e "success\n"; else echo -e "fail\n"; exit 1; fi;
}

function test_append() {
    file_size=$1
    bs_size=$(($1 *1024 *1024))  # 1 MB
    echo "测试对已有文件追加写 $1 MB 内容 (不超出配额)"

    func_get_quota_used; old_used=${global_used}
    
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size} count=1 oflag=append conv=notrunc";
    echo -e "exec cmd:\n\t ${cmd}"; eval ${cmd} &> /dev/null
    
    func_get_quota_used; new_used=${global_used}
    expect_used=$((${old_used} + ${bs_size}))

    eval echo -n -e "==${test_num}==\${msg$test_num}==== "
    if ((${old_used} != 0)) && (( ${new_used} == ${expect_used})); then
        echo -e "success\n"; else echo -e "fail\n"; exit 1; fi
}

function test_append_exceed() {
    file_size=$1
    bs_size=$((${file_size} *1024 *1024)) #12 MB
    echo "测试对已有文件追加写 $1 MB 内容 (超出配额)"
    
    func_get_quota_used; old_used=${global_used}
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size} count=1 oflag=append conv=notrunc";
        echo -e "exec cmd:\n\t ${cmd}"; eval ${cmd} &> ${LOGFILE}
    
    exceed=`cat ${LOGFILE} | grep "Disk quota exceeded" | wc -l`
        if (( ${exceed} == 1 )); then echo -e "\t Error: Disk quota exceeded"; fi
    
    func_get_quota_used; new_used=${global_used}
    expect_used=${old_used}

    eval echo -n -e "==${test_num}==\${msg$test_num}==== "
    if (( ${new_used} == ${expect_used})) && (( ${exceed} == 1 )); then
        echo -e "success\n"; else echo -e "fail\n"; exit 1; fi
}

function test_open_with_trunc() {
    bs_size=$(($1 *1024 *1024)) # 5MB
    echo "测试对已有文件覆盖写 $1 MB 内容 (不超出配额)"
    
    func_get_quota_used; old_used=${global_used}
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size} count=1";echo -e "exec cmd:\n\t ${cmd}"; eval ${cmd} &> ${LOGFILE}
    func_get_quota_used; new_used=${global_used}

    eval echo -n -e "==${test_num}==\${msg$test_num}===="
    if ((${old_used} != 0)) && (( ${new_used} == ${bs_size})); then
        echo -e "success\n"; else echo -e "fail\n"; exit 1; fi
}

function test_open_with_trunc_exceed() {
    bs_size=$(($1 *1024 *1024)) # 12MB
    echo "测试对已有文件覆盖写 $1 MB 内容 (超出配额)"

    func_get_quota_used; old_used=${global_used}
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size} count=1";echo -e "exec cmd:\n\t ${cmd}"; eval ${cmd} &> ${LOGFILE}
    exceed=`cat ${LOGFILE} | grep "Disk quota exceeded" | wc -l`
    if (( ${exceed} == 1 )); then echo -e "\t Error: Disk quota exceeded";fi
    
    func_get_quota_used; new_used=${global_used}

    eval echo -n -e "==${test_num}==\${msg$test_num}===="
    if ((${old_used} != 0)) && (( ${new_used} == 0)); then
        echo -e "success\n"; else echo -e "fail\n"; exit 1; fi
}

echo "/*******************************"
echo "   测试《非空》配额目录写文件     "
echo "*******************************/"
if [ "$1" == "--help" ] || [ -z "$1" ];then print_help; fi

# init global quota env and check env
source ${QUOTA_COMMON_FUNC}

init_all 10; #MB
#set -x;
test_num=1; if (($1 == ${test_num})) || (($1 == 0));then test_append 1; fi #1 MB
test_num=2; if (($1 == ${test_num})) || (($1 == 0));then test_append_exceed 12; fi #12 MB
test_num=3; if (($1 == ${test_num})) || (($1 == 0));then test_open_with_trunc 5; fi #5 MB
test_num=4; if (($1 == ${test_num})) || (($1 == 0));then test_open_with_trunc_exceed 12; fi #12 MB

echo "/*******************************"
echo "测试《非空》配额目录写文件全部成功 "
echo "*******************************/"
exit 0;

